package com.voltmao.controller.module.contract;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.voltmao.service.application.contract.ProviderAuditServiceImpl;
import com.voltmao.service.application.contract.ProviderServiceImpl;
import com.voltmao.webpro.frame.analyze.DTOSmith;
import com.voltmao.webpro.frame.dto.ApiResult;
import com.voltmao.webpro.frame.dto.DataPage;
import com.voltmao.webpro.frame.dto.GridViewResult;
import com.voltmao.webpro.frame.dto.status.Constants;
import com.voltmao.webpro.frame.exception.WebProViewException;
import com.voltmao.webpro.utils.http.HttpRequestUtil;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 合同控制层
 *
 * @author zhanglin
 * @date 2018/04/10
 */
@Controller
@RequestMapping(value = "/module/contract/provider")
public class ProviderController {

    @Autowired
    ProviderServiceImpl providerService;
    @Autowired
    ProviderAuditServiceImpl providerAuditService;

    @ResponseBody
    @RequestMapping(value = "/apiInsertOrUpdate.json", method = RequestMethod.POST)
    public ApiResult insertOrUpdate(HttpServletRequest request){
        /*
         * 新增或修改合同和sdnx、gdbc、maps等表
         */
        return new DTOSmith(request).builderApiResult((request1, apiResult) -> {
            Map<String, Object> params = HttpRequestUtil.getParameterMap(request1);
            System.out.println(params.toString());
            try {
                providerService.insertOrUpdateContractArgs(params);
                apiResult.setStatus(Constants.SUCCESS);
            } catch (IllegalArgumentException e){
                apiResult.status = Constants.EXTERNA_ERROR.toString();
                apiResult.status = "渠道商审核未通过";
            } catch (IllegalStateException e){
                apiResult.setStatus(Constants.FORBID_ERROR);
            } catch (Exception e) {
                e.printStackTrace();
                apiResult.setStatus(Constants.UPDATE_FAIVAL);
            }
            return null;
        });
    }

    @ResponseBody
    @RequestMapping("/apiDelete.json")
    public ApiResult deleteById(HttpServletRequest request){
        /*
         * 根据合同id删除相关的表
         */
        return new DTOSmith(request).builderApiResult((request1, apiResult) -> {
            Map<String, Object> params = HttpRequestUtil.getParameterMap(request1);
            try {
                providerService.deleteContractProvider(params.get("dataId") + "");
                apiResult.setStatus(Constants.SUCCESS);
            } catch (IllegalStateException e){
                apiResult.setStatus(Constants.FORBID_DELETE);
            }
            catch (Exception e) {
                e.printStackTrace();
                apiResult.setStatus(Constants.UPDATE_FAIVAL);
            }
            return null;
        });
    }

    @ResponseBody
    @RequestMapping(value = "/gridList.json", method = RequestMethod.POST)
    public GridViewResult testContractGrid(HttpServletRequest request) {
        return new DTOSmith(request).builderLoadGridViewResult(new DTOSmith.LoadGridViewBackCall() {
            @Override
            public String getDataUrl(HttpServletRequest request) {
                return "/module/contract/provider/gridList.json";
            }

            @Override
            public DataPage resultData(HttpServletRequest request, GridViewResult apiResult) throws WebProViewException {
                Map<String, Object> params = HttpRequestUtil.getParameterMap(request);
                try {
                    apiResult.status = Constants.SUCCESS.toString();
                    apiResult.msg = Constants.SUCCESS.getDes();
                    return providerService.selectContractDataPage(params);
                } catch (NullPointerException e){
                    apiResult.status = Constants.SEARCH_ERROR.toString();
                    apiResult.msg = Constants.SEARCH_ERROR.getDes();
                    return null;
                } catch (Exception e){
                    e.printStackTrace();
                    apiResult.status = Constants.PARAMETER_ERROR.toString();
                    apiResult.msg = Constants.PARAMETER_ERROR.getDes();
                    return null;
                }
            }
            @Override
            public Object defalutForm(HttpServletRequest request) throws WebProViewException {
                return null;
            }
        });
    }
}
